package router

import (
	"exchangeapp/controllers"
	"exchangeapp/middlewares"
	"time"

	"strings"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func SetupRouter() *gin.Engine {
	r := gin.Default()

	r.Use(cors.New(cors.Config{
		AllowMethods:     []string{"GET", "POST"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return strings.HasPrefix(origin, "http://localhoost:")
		},
		MaxAge: 10 * time.Hour,
	}))

	auth := r.Group("/api/auth")
	{
		auth.POST("/login", controllers.Login)
		auth.POST("/register", controllers.Register)
	}

	api := r.Group("/api")
	api.GET("/exchange", controllers.GetExchangeRate)
	api.Use(middlewares.Auth())
	{
		api.POST("/exchange", controllers.CreateExchangeRate)
		api.GET("/article", controllers.GetArticle)
		api.GET("/article/:id", controllers.GetArticleByID)
		api.POST("/article", controllers.CreateArticle)
		api.GET("/article/:id/likes", controllers.LikeArticle)
		api.POST("/article/:id/likes", controllers.GetArticleLikes)
	}

	return r
}
